package com.goodfather.textbook.youdao.pad.pac;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.goodfather.textbook.youdao.pad.R;
import com.goodfather.textbook.youdao.pad.bean.Lessons;
import com.goodfather.textbook.youdao.pad.util.ImageUtils;
import com.goodfather.textbook.youdao.pad.weight.RecyclerViewBaseAdapter;

import java.util.List;

public class BookCatalogAdapter extends RecyclerViewBaseAdapter<Lessons> {

    private ItemClickListener mListener;
    private int mCurPosition = 0;
    private boolean mPurchased;

    public BookCatalogAdapter(Context context, List<Lessons> data) {
        super(context, data);

    }

    public void setData(List data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(ItemClickListener listener) {
        mListener = listener;
    }

    public void setCurPosition(int position) {
        mCurPosition = position;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        return new CatalogHolder(mInflater.inflate(R.layout.item_book_catalog, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
        if (mData == null || mData.size() == 0) return;
        if (viewHolder instanceof CatalogHolder) {
            CatalogHolder catalogHolder = (CatalogHolder) viewHolder;

            if (mCurPosition == position) {
                catalogHolder.fl_book.setSelected(true);
                catalogHolder.tv_isStudy.setVisibility(View.VISIBLE);
            } else {
                catalogHolder.fl_book.setSelected(false);
                catalogHolder.tv_isStudy.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(mData.get(position).lessonName)){
                catalogHolder.tv_cataName.setText(mData.get(position).unitName);
            }else if (TextUtils.isEmpty(mData.get(position).unitName)){
                catalogHolder.tv_cataName.setText(mData.get(position).lessonName);
            }else {
                catalogHolder.tv_cataName.setText(mData.get(position).unitName + "  " + mData.get(position).lessonName);
            }

            if (mPurchased){
                catalogHolder.iv_lock.setVisibility(View.GONE);
            }else{
                if (mData.get(position).isFree){
                    catalogHolder.iv_lock.setVisibility(View.GONE);
                }else{
                    catalogHolder.iv_lock.setVisibility(View.VISIBLE);
                }
            }
            ImageUtils.loadImage(mData.get(position).imageUrl,catalogHolder.iv_catalog);
            catalogHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onClick(mData.get(position), position);
                    }
                }
            });
        }

    }

    public void setPurchased(boolean purchased){
        mPurchased = purchased;
    }

    public interface ItemClickListener {
        void onClick(Lessons lessons, int position);
    }

    public class CatalogHolder extends RecyclerView.ViewHolder {
        private final ImageView iv_lock;
        private FrameLayout fl_book;
        private TextView tv_cataName;
        private TextView tv_isStudy;
        private ImageView iv_catalog;

        public CatalogHolder(@NonNull View itemView) {
            super(itemView);
            fl_book = (FrameLayout) itemView.findViewById(R.id.fl_book);
            tv_cataName = (TextView) itemView.findViewById(R.id.tv_cataName);
            tv_isStudy = (TextView) itemView.findViewById(R.id.tv_isStudy);
            iv_catalog = (ImageView) itemView.findViewById(R.id.iv_catalog);
            iv_lock = (ImageView) itemView.findViewById(R.id.iv_lock);
        }
    }
}
